
global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/figure_5b_hartz_eventstudy_b.log, replace name(fig)

capture noi {

    * Hartz reform figure - panel B

    clear
    set scheme plotplain
    
    * set parameters
    global target "DE"
    local y_target "2003"
    local rangecal "1995(1)2012"
    local ycondition "if year>=1995 & year <= 2012"
    local pauto "pauto95"

    * prepare data: load baseline reg data, drop domestic firms and maxshare in target country firms, keep relevant variables
    use ${final_dir}/regression_dataset_from1970_tfacit1.dta, clear
    keep if country_shr_1995 != "${target}"
    keep if missing_weights_1995 == 0 & maxweight_1995 < 1
    mmerge BvD using ${dataset_dir}/weights/bvdid_pat_weights_EPtr_from1970_1995_orbis2017_tfacit1.dta, ukeep(share2_all_1995_${target}) unmatched(master)
    ren share2_all_1995_${target} weight_${target}
    ren country_shr_1995 country
    gen target_country = "${target}"
    keep BvDIDnumber lse_id industry year ${depvar}_${ttt} pauto90_${ttt} pauto95_${ttt} k${depvar}_${ttt}* kpauto90_${ttt}* kpauto95_${ttt}* kany_${ttt}* country target_country weight_${target}

    * build firm-innovation types (expand dataset by auto / pauto)
    expand 2, gen(autodummy)
    gen auto_innovation = 0
    replace auto_innovation = ${depvar}_${ttt} if autodummy == 1 
    gen pauto_innovation = 0
    replace pauto_innovation = `pauto'_${ttt} if autodummy == 0 
    gen innovations = auto_innovation + pauto_innovation
    drop auto_innovation pauto_innovation 
    gen BvDinnovation = BvDIDnumber + "-auto" if autodummy == 1 
    replace BvDinnovation = BvDIDnumber + "-placebo" if autodummy == 0 
    egen firm_auto = group(BvDinnovation)
    xtset firm_auto year

    * define stocks
    gen stockauto = k${depvar}_${ttt}
    gen stockautozero = k${depvar}_${ttt}0
    gen stockplacebo = k`pauto'_${ttt}
    gen stockplacebozero = k`pauto'_${ttt}0
    gen stockany = kany_${ttt}
    gen stockanyzero = kany_${ttt}0

    * build other stock
    foreach stock in stockauto stockplacebo stockany {
        gen exp_`stock' = exp(`stock')
        replace exp_`stock' = 0 if `stock'zero == 1
    }
    gen stockother = ln(exp_stockany - exp_stockauto - exp_stockplacebo)
    replace stockother = 0 if stockother == .
    gen stockotherzero = (stockother = =0)
    drop exp_* stockany*

    * build innovationtype x firm stock 
    foreach stock in stockauto stockplacebo stockother {
        gen auto_`stock' = autodummy * `stock'
    }

    * build country-innovationtype-year FE
    egen country_auto_year = group(country year autodummy)

    * build industry-year-innovationtype FE
    egen industry_auto_year = group(industry year autodummy)

    * build exposure-year FE
    forvalues i = `rangecal' {
        gen YY_`i' = 1 if year == `i' 
        replace YY_`i' = 0 if YY_`i' ==. 
        gen exposure_year`i' = weight_${target} * YY_`i'
        drop YY_`i'
    }
    replace exposure_year`y_target'= 0

    * build exposure-year-auto
    forvalues i = `rangecal' {
        gen exposure_auto_year`i' = exposure_year`i' * autodummy
    }
    ren exposure_auto_year* beta_*

    ppmlhdfe F2.innovations beta_* stockauto stockautozero stockplacebo stockplacebozero stockother stockotherzero auto_stockauto auto_stockplacebo auto_stockplacebo exposure_year* `ycondition', absorb(firm_auto country_auto_year industry_auto_year) vce(cluster lse_id)

    * plot betas
    mat b = e(b)
    mat V = e(V)
    mat b = b[1,"beta_1995".."beta_2012"]
    mat V = V["beta_1995".."beta_2012","beta_1995".."beta_2012"]
    mat conf = J(2,`=colsof(b)',.)

    forvalues i = 1(1)`=colsof(b)' {
        mat conf[1,`i'] = b[1,`i'] + invnormal(0.975)*sqrt(V[`i',`i'])
        mat conf[2,`i'] = b[1,`i'] - invnormal(0.975)*sqrt(V[`i',`i'])
    }

    mata : st_matrix("conf_max",rowmax(st_matrix("conf")[1,.]))
    mata : st_matrix("conf_min",rowmin(st_matrix("conf")[2,.]))


    qui include ${code_dir}/config/figuretools.do
    colorpalette ptol, n(1) nograph
    coefplot, omitted keep(beta_1995 beta_1996 beta_1997 beta_1998 beta_1999 beta_2000 beta_2001 beta_2002 beta_2003 ///
        beta_2004 beta_2005 beta_2006 beta_2007 beta_2008 beta_2009 beta_2010 beta_2011 beta_2012) vertical xlabel(1 "1997" 3 "1999" 5 "2001" 7 "2003" 9 "2005" 11 "2007" 13 "2009" 15 "2011" 17 "2013")  xscale(r(1 17)) ///
        msymbol("solid") recast(connected) ciopts(lwidth(0.2) recast(rarea) color("68 119 170%20") lwidth(none)) ///
        msymb(circle) mcol("68 119 170") lcolor("68 119 170")  ///
        yline(0,lcolor(gs8)) ytitle("Coefficient") xtitle("Year") ylabel(-4 "-4" -2 "-2" 0 "0" 2 "2" 4 "4") yscale(r(-5.75 4)) ///
        text(-4  8 "{it:Hartz}" "{it:reforms}", just(center)) ///
        addplot(pcarrowi -4.8 7 -4.8 9, recast(pcbarrow) lwidth(thin) mcolor(gs4) barbsize(0) lcolor(gs4), below)
    graph export ${fig_dir}/main/Figure_5b_hartz_eventstudy_b.pdf, as(pdf) replace


    *eps
    coefplot, omitted keep(beta_1995 beta_1996 beta_1997 beta_1998 beta_1999 beta_2000 beta_2001 beta_2002 beta_2003 ///
        beta_2004 beta_2005 beta_2006 beta_2007 beta_2008 beta_2009 beta_2010 beta_2011 beta_2012) vertical xlabel(1 "1997" 3 "1999" 5 "2001" 7 "2003" 9 "2005" 11 "2007" 13 "2009" 15 "2011" 17 "2013")  xscale(r(1 17)) ///
        msymbol("solid") recast(connected) ciopts(lwidth(0.2) recast(rarea) color("68 119 170%20") lwidth(none)) ///
        msymb(circle) mcol("68 119 170") lcolor("68 119 170")  ///
        yline(0,lcolor(gs8)) ytitle("Coefficient") xtitle("Year") ylabel(-4 "-4" -2 "-2" 0 "0" 2 "2" 4 "4") yscale(r(-5.75 4)) ///
        text(-4  8 "{it:Hartz}" "{it:reforms}", just(center)) ///
        addplot(pcarrowi -4.8 7 -4.8 9, recast(pcbarrow) lwidth(thin) mcolor(gs4) barbsize(0) lcolor(gs4), below)
    graph export ${fig_dir}/main/fig5b.eps, as(eps) replace



    * report numbers that we state in paper
    log using ${numb_dir}/Figure_5b_hartz_eventstudy_b_numbers.log, replace name(numbers)
    keep if e(sample)
    duplicates drop BvDIDnumber, force
    count 
    log close numbers

}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close fig